/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import dao.DepartmentDAO;
import dao.DesignationDAO;
import dao.EmployeeDAO;
import dao.SectionDAO;
import data.Departments;
import data.Employees;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ComboBoxModel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.hibernate.HibernateException;
import view.ManageEmployee;

/**
 *
 * @author Hùng
 */
public class EmployeeBO extends Error {

    private String Success;
    private Pattern pattern;
    private Matcher matcher;

    public EmployeeBO() {
    }

    public EmployeeBO(String Success) {
        this.Success = Success;
    }

    public String getSuccess() {
        return Success;
    }

    public void setSuccess(String Success) {
        this.Success = Success;
    }

    public boolean validate(final String email) {
        pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        matcher = pattern.matcher(email);
        return matcher.matches();

    }

    public List<Employees> showALLEmployee() throws Exception {

        List<Employees> lEmployees = new EmployeeDAO().getallEmployee();

        return lEmployees;

    }

    public void registerEmployee(Object tmpdeptname, Object tmpnameSec, Object tmpnameDesign, String nameE, String usernameE, char[] password, Object tmpRole, String addressE, String phoneE, String faxE, String emailE, String idE, String tmpflag) throws Error {

        if (tmpdeptname != null && tmpnameDesign != null && !nameE.equals("") && !usernameE.equals("") && password.length > 5 && tmpRole != null && !addressE.equals("") && !phoneE.equals("") && !faxE.equals("") && !emailE.equals("")) {
            //check email
            if (validate(emailE)) {
                //check setion
                if ((tmpflag.equals("0") && tmpnameSec == null) || (!tmpflag.equals("0") && tmpnameSec != null)) {
                    //check username
                    Employees tmpE = null;
                    tmpE = new EmployeeDAO().getEbyUserName(usernameE);
                    if (tmpE == null) {
                        String deptname =  (String) tmpdeptname;
                        Departments tmpDept =new DepartmentDAO().getDepartmentByName(deptname);
                        String nameDesign = (String) tmpnameDesign;
                        String nameSec = null;
                        String passE = new String(password);
                        int Role = 0;
                        switch ((String) tmpRole) {
                            case "Admin":
                                Role = 1;
                                break;
                            case "Hrmanager":
                                Role = 2;
                                break;
                            case "Employee":
                                Role = 3;
                                break;
                        }
                        Employees tmpEmloyee = new Employees(
                                "E" + Calendar.getInstance().getTimeInMillis(),
                                new DesignationDAO().getDesignationbyDesignation(nameDesign),
                                null,
                                nameE,
                                tmpDept.getDeptno(),
                                addressE,
                                phoneE,
                                faxE,
                                emailE,
                                usernameE,
                                passE, Role, null, null);
                        if (tmpnameSec != null) {
                            nameSec = (String) tmpnameSec;
                            tmpEmloyee.setSections(new SectionDAO().getSectionbyNameSec(nameSec));
                        }
                        // insert employee
                        boolean flag = new EmployeeDAO().executeInsertHQLQuery(tmpEmloyee);
                        if (flag == true) {
                            Success = "Register Employee SuccessFully";
                        } else {
                            throw new Error("Register Employee Fail");
                        }
                    } else {
                        throw new Error("Please,Your UserName is existed");
                    }
                } else {
                    throw new Error("Please,check your Section");
                }
            } else {
                throw new Error("Please,check your Email");
            }
        } else {
            throw new Error("Please,check your information");
        }

    }

    public void removeEmployees(int[] selectedRows, TableModel model, String idE) {

        if (selectedRows.length > 0) {
            try {
                List<Employees> lEmp = new ArrayList<Employees>();
                for (int i = 0; i < selectedRows.length; i++) {
                    Employees tmpE = new EmployeeDAO().getEbyIdE((String) model.getValueAt(selectedRows[i], 0));
                    if (tmpE.getJobrotations().size() > 0) {
                        throw new Error("Delete Employees Fail.Please,Delete List Jobrotations detail of Employee");
                    } else if (tmpE.getVacancyfilldetailses().size() > 0) {
                        throw new Error("Delete Employees Fail.Please,Delete List Vacancy Fill detail  of Employee");
                    }
                    lEmp.add(tmpE);
                }
                for (Employees a : lEmp) {
                    new EmployeeDAO().executeDeleteHQLQuery(a);
                }
                Success = "Delete Employees SuccessFully";
            } catch (HibernateException he) {
                throw new Error("Delete Employees Fail." + he.getMessage());

            }




        } else {
            throw new Error("Please,Select Rows on Table to delete");
        }

    }

    public void showInformationofAnEmp(int[] selectedRows, TableModel model, ComboBoxModel cbDept, ComboBoxModel cbSec, ComboBoxModel cbDesign, ComboBoxModel cbRole, JTextField IdE, JTextField NameE, JTextField UserE, JPasswordField PassE, JTextField AddressE, JTextField PhoneE, JTextField FaxE, JTextField EmailE) {
        if (selectedRows.length == 1) {
            String tmpUserE = (String) model.getValueAt(selectedRows[0], 2);
            Employees tmpE = new EmployeeDAO().getEbyUserName(tmpUserE);

            ManageEmployee.setsEmp(tmpE);
        } else {
            throw new Error("Please,Select only Row on Table to edit");
        }
    }

    public void editEmployee(Object tmpdeptname, Object tmpnameSec, Object tmpnameDesign, String nameE, String usernameE, char[] password, Object tmpRole, String addressE, String phoneE, String faxE, String emailE, String idE, String tmpflag) throws Error {

        if (tmpdeptname != null && tmpnameDesign != null && !nameE.equals("") && !usernameE.equals("") && password.length > 5 && tmpRole != null && !addressE.equals("") && !phoneE.equals("") && !faxE.equals("") && !emailE.equals("")) {
            //check email
            if (validate(emailE)) {
                //check setion
                if ((tmpflag.equals("0") && tmpnameSec == null) || (!tmpflag.equals("0") && tmpnameSec != null)) {
                    //check username
                    Employees tmpE1 = null;
                    tmpE1 = new EmployeeDAO().getEbyUserName(usernameE);
                    Employees tmpE = new EmployeeDAO().getEbyIdE(idE);
                    if (tmpE1 == null || tmpE.getUsername().equals(usernameE)) {
                        String deptname =  (String) tmpdeptname;
                        Departments tmpDept =new DepartmentDAO().getDepartmentByName(deptname);
                        String nameDesign = (String) tmpnameDesign;
                        String nameSec = null;
                        String passE = new String(password);
                        int Role = 0;
                        switch ((String) tmpRole) {
                            case "Admin":
                                Role = 1;
                                break;
                            case "Hrmanager":
                                Role = 2;
                                break;
                            case "Employee":
                                Role = 3;
                                break;
                        }
                        Employees tmpEmloyee = new Employees(
                                idE,
                                new DesignationDAO().getDesignationbyDesignation(nameDesign),
                                null,
                                nameE,
                                tmpDept.getDeptno(),
                                addressE,
                                phoneE,
                                faxE,
                                emailE,
                                usernameE,
                                passE, Role, null, null);
                        if (tmpnameSec != null) {
                            nameSec = (String) tmpnameSec;
                            tmpEmloyee.setSections(new SectionDAO().getSectionbyNameSec(nameSec));
                        }
                        // insert employee
                        boolean flag = new EmployeeDAO().executeUpdateHQLQuery(tmpEmloyee);
                        if (flag == true) {
                            Success = "Edit Employee SuccessFully";
                        } else {
                            throw new Error("Edit Employee Fail");
                        }
                    } else {
                        throw new Error("Please,Your UserName is existed");
                    }
                } else {
                    throw new Error("Please,check your Section");
                }
            } else {
                throw new Error("Please,check your Email");
            }
        } else {
            throw new Error("Please,check your information");
        }

    }

    public Employees findEmpbyIdE(String text) {
        Employees tmpE = new EmployeeDAO().findEmpbyIdE(text);
        return tmpE;
       }
}
